﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Web.Security;

namespace mepostras.BO.Util
{
    public class BoCommon
    {
        /// <summary>
        /// Bỏ dấu để làm Alias
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ConvertToUnSign(string s)
        {
            var regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string temp = s.Normalize(NormalizationForm.FormD);
            temp = regex.Replace(temp, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').ToLower();

            // Xóa ký tự thừa
            // Chuyển sang khoảng trắng => Xóa khoảng trắng thừa => Chuyển từ khoảng trắng sang -
            temp = Regex.Replace(temp, "[^a-zA-Z0-9]", " ");
            temp = Regex.Replace(temp, @"\s+", " ").Trim();
            temp = temp.Replace(@" ", "-");
            return temp;
        }

        public static bool IsValidImage(string fileName)
        {
            var regex = new Regex(@"(.*?)\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF|bmp|BMP)$");
            return regex.IsMatch(fileName);
        }

        /// <summary>
        /// Tạo Password cùng với passwordsalt
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string CreatePasswordHash(string pwd, string salt)
        {
            string saltAndPwd = String.Concat(pwd, salt);
            string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
            return hashedPwd;
        }

        /// <summary>
        /// Tạo passwordsalt
        /// </summary>
        /// <returns></returns>
        public static string CreateSalt()
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[32];
            rng.GetBytes(buff);

            return Convert.ToBase64String(buff);
        }
        /// <summary>
        /// Tạo alias từ chuỗi title tiếng việt
        /// </summary>
        /// <param name="VietnameseSource"></param>
        /// <returns></returns>
        public static String RemoveVietnameseSigns(String VietnameseSource)
        {
            VietnameseSource = VietnameseSource.Trim();
            try
            {

                String[] SpecialSign = { ":", "-", "- ", " -", "'", ";", ",", ".", "\"", "?", "“", "”" };
                for (Int16 index = 0; index < SpecialSign.Length; index++)
                {
                    VietnameseSource = VietnameseSource.Replace(SpecialSign[index], "");
                }
                String[] VietnameseSigns = new String[] { "aAeEoOuUiIdDyY", "áàạảãâấầậẩẫăắằặẳẵ", "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ", "éèẹẻẽêếềệểễ", "ÉÈẸẺẼÊẾỀỆỂỄ", "óòọỏõôốồộổỗơớờợởỡ", "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ", "úùụủũưứừựửữ", "ÚÙỤỦŨƯỨỪỰỬỮ", "íìịỉĩ", "ÍÌỊỈĨ", "đ", "Đ", "ýỳỵỷỹ", "ÝỲỴỶỸ" };
                for (int i = 1; i < VietnameseSigns.Length; i++)
                {
                    for (int j = 0; j < VietnameseSigns[i].Length; j++)
                    {
                        VietnameseSource = VietnameseSource.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
                    }
                }
                VietnameseSource = VietnameseSource.Replace(" ", "-");
                return VietnameseSource;
            }
            catch (Exception)
            {
                return VietnameseSource;
            }
        }
    }
}
